#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#include "logger.h"
#include "parser.h"
#include "bureaucrat.h"
#include "registrar.h"
#include "schedule.h"
#include <sstream>
#include <boost/lexical_cast.hpp>
#ifdef HAVE_UNISTD_H
#include <unistd.h>

void options(int, char**);

#endif

int main(int argc, char *argv[])
{
#ifdef HAVE_UNISTD_H
	options(argc, argv);
#endif

	Parser* parser = Parser::instance();
	
	// We're hardcoding the files, for now.
	parser->prefix("./data");
	parser->courses("courses.xml");
	parser->student("student.xml");

	if(parser->parse())
		Logger::instance()->message("Success!", 1);
	else {
		Logger::instance()->error("Failed!", 1);
		return -1;
	}

	Registrar::instance()->student().setCourseCount(4);

	std::vector<Schedule> scheds = Bureaucrat::instance()->schedules();
	
	Schedule s = scheds[0];
	std::cout << s  <<std::endl;

	return 0;
}

#ifdef HAVE_UNISTD_H
void options(int argc, char *argv[])
{
	using boost::lexical_cast;
	char *verbosity=0;
	
	int c;
	std::stringstream ss;
	opterr = 0;
     
	while ((c = getopt (argc, argv, "v:")) != -1)
		switch (c)
			{
			case 'v':
				verbosity = optarg;
				break;
			case '?':
				if (optopt == 'v')
					{
						ss << "Option -"<< (char)optopt << " requires an argument." ;
						Logger::instance()->error(ss.str(), 0);
					}
				else if (isprint (optopt))
					{
						ss << "Unknown option `-"<< (char)optopt<<"'.";
						Logger::instance()->error(ss.str(), 0);
					}
				else
					{
						ss<<  "Unknown option character `\\x"<<std::hex<<optopt<<"'." ;
						Logger::instance()->error(ss.str(), 0);
					}
				exit(1);
				break;
			default:
				abort();
			}

	if(verbosity)
		Logger::instance()->level(lexical_cast<int>(verbosity));


}
#endif
